package com.hsc.www.reSend.core.delayedQueue;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultCapillaryDelayedQueueFactory implements CapillaryDelayedQueueFactory {

    private final Map<Long, CapillaryDelayedQueue> delayedQueueCache = new ConcurrentHashMap();

    public DefaultCapillaryDelayedQueueFactory(List<CapillaryDelayedQueue> queueList) {
        if (!Objects.isNull(queueList)) {
            queueList.forEach(v -> delayedQueueCache.put(v.getTimeUnit().toSeconds(v.getDelayedTime()), v));
        }
    }

    /**
     * 挑选合适的延迟队列
     *
     * @param delayedTime 延迟时间 单位：秒
     */
    @Override
    public CapillaryDelayedQueue pick(Integer delayedTime) {
        return delayedQueueCache.get(Long.valueOf(delayedTime));
    }

    /**
     * 获取所有的延迟队列
     */
    @Override
    public Collection<CapillaryDelayedQueue> totalQueues(){
       return delayedQueueCache.values();
    }
}
